<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>AI对话式模拟面试</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body { background: #f5f7fa; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; }
        .container { max-width: 700px; margin: 40px auto; background: #fff; border-radius: 18px; box-shadow: 0 4px 20px #0071e344; padding: 32px 24px; }
        h2 { text-align: center; font-size: 26px; font-weight: 700; color: #0071e3; margin-bottom: 24px; }
        .chat-box { background: #f0f7ff; border-radius: 12px; padding: 18px; color: #333; font-size: 17px; margin-bottom: 18px; min-height: 180px; }
        .question { color: #0071e3; font-weight: 600; margin-bottom: 8px; }
        .answer { color: #333; margin-bottom: 12px; }
        .input-area { display: flex; gap: 12px; margin-top: 12px; }
        .input-area input { flex: 1; padding: 10px; border-radius: 8px; border: 1px solid #e0e0e0; font-size: 16px; }
        .input-area button { background: #0071e3; color: #fff; border: none; border-radius: 8px; padding: 8px 18px; font-size: 16px; cursor: pointer; }
        .report { background: #e6f7ff; border-radius: 12px; padding: 18px; color: #333; font-size: 16px; margin-top: 24px; }
        .back-link { display: inline-block; margin-top: 24px; color: #0071e3; text-decoration: underline; font-size: 16px; }
        .brand-bar { width:100%;background:linear-gradient(90deg,#0071e3 60%,#00c3a0 100%);padding:18px 0 10px 0;box-shadow:0 2px 12px rgba(0,113,227,0.08);margin-bottom:18px; }
        .brand-bar-inner { max-width:900px;margin:0 auto;display:flex;align-items:center; }
        .brand-logo { width:44px;height:44px;border-radius:50%;background:#fff;display:flex;align-items:center;justify-content:center;margin-right:14px;box-shadow:0 2px 8px rgba(0,113,227,0.10); }
        .brand-logo span { font-size:26px;font-weight:bold;color:#0071e3; }
        .brand-title { font-size:22px;font-weight:700;color:#fff;letter-spacing:2px; }
        .brand-desc { margin-left:24px;font-size:16px;color:#e0f7fa;font-weight:500; }
    </style>
</head>
<body>
    <div class="brand-bar">
        <div class="brand-bar-inner">
            <div class="brand-logo"><span>育</span></div>
            <span class="brand-title">育聘智联</span>
            <span class="brand-desc">AI对话式模拟面试</span>
        </div>
    </div>
    <div class="container">
        <h2><i class="fas fa-comments"></i> AI对话式模拟面试</h2>
        <div class="chat-box" id="chatBox"></div>
        <div class="input-area">
            <input type="text" id="answerInput" placeholder="请输入你的答案..." autocomplete="off" />
            <button onclick="submitAnswer()">提交答案</button>
        </div>
        <div class="report" id="reportBox" style="display:none;"></div>
        <a href="/resume/{{ resume_id }}" class="back-link"><i class="fas fa-arrow-left"></i> 返回简历详情</a>
    </div>
    <script>
        let chatHistory = [];
        let currentQuestion = 0;
        let finished = false;
        const resumeId = "{{ resume_id }}";
        function renderChat() {
            const box = document.getElementById('chatBox');
            box.innerHTML = chatHistory.map(item => `<div class='question'>${item.q}</div>${item.a ? `<div class='answer'>${item.a}</div>` : ''}${item.analysis ? `<div style='color:#888;font-size:15px;margin-bottom:8px;'>AI点评：${item.analysis}</div>` : ''}`).join('');
        }
        function submitAnswer() {
            if(finished) return;
            const input = document.getElementById('answerInput');
            const answer = input.value.trim();
            if(!answer) return;
            input.value = '';
            // 新增：提交后立即显示“正在解析答案...”
            document.getElementById('chatBox').innerHTML += '<div style="color:#888;">AI正在解析你的答案，请稍候...</div>';
            fetch(`/api/interview_chat`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ resume_id: resumeId, question_idx: currentQuestion, answer, history: chatHistory })
            }).then(res => res.json()).then(data => {
                if(data.success) {
                    // 只更新当前题的答案和点评，不重复追加首题
                    if(chatHistory.length > 0 && !chatHistory[chatHistory.length-1].a) {
                        chatHistory[chatHistory.length-1] = { q: data.question, a: answer, analysis: data.analysis };
                    } else {
                        chatHistory.push({ q: data.question, a: answer, analysis: data.analysis });
                    }
                    currentQuestion = data.next_idx;
                    renderChat();
                    if(data.finished) {
                        finished = true;
                        document.getElementById('answerInput').disabled = true;
                        document.getElementById('reportBox').style.display = 'block';
                        // 新增：最后一题答完后提示报告生成中
                        document.getElementById('reportBox').innerHTML = `<div style='color:#0071e3;font-size:17px;margin-bottom:10px;'>AI面试报告正在生成中，请稍候...</div>`;
                        setTimeout(() => {
                            document.getElementById('reportBox').innerHTML = `<b>AI面试报告：</b><br>${data.report}`;
                        }, 1200);
                    } else {
                        // 下一题
                        document.getElementById('chatBox').innerHTML += '<div style="color:#888;">AI正在生成下一个面试问题，请稍候...</div>';
                        setTimeout(() => {
                            chatHistory.push({ q: data.next_question });
                            renderChat();
                        }, 600);
                    }
                }
            });
        }
        // 初始化第一题
        window.onload = function() {
            document.getElementById('chatBox').innerHTML = '<span style="color:#888;">AI正在生成面试问题，请稍候...</span>';
            fetch(`/api/interview_chat?resume_id=${resumeId}`)
                .then(res => res.json())
                .then(data => {
                    if(data.success && data.question) {
                        chatHistory = [{ q: data.question }];
                        currentQuestion = 0;
                        finished = false;
                        renderChat();
                    } else {
                        document.getElementById('chatBox').innerHTML = '<span style="color:#888;">未能获取AI面试问题，请稍后重试。</span>';
                    }
                })
                .catch(err => {
                    document.getElementById('chatBox').innerHTML = '<span style="color:#888;">AI接口异常，请检查网络或后端服务。</span>';
                });
        }
    </script>
</body>
</html>
